﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Drawing.Printing;
using System.Reflection;
using Model;

namespace Sales
{
    public partial class PrintShowDialog : Form
    {
        public PrintShowDialog()
        {
            InitializeComponent();
        }

        public PrintDocument Document = new PrintDocument();

        #region 打印条形码

        //鼠标滚轮和拖动支持变量
        private System.Reflection.FieldInfo m_Position;
        private System.Reflection.MethodInfo m_SetPositionMethod;
        private bool isMouseDown;
        private Point startPosition;
        private Point endPosition;
        private Point curPos;
        private static XmlModel xmlModel;
        /// <summary>
        /// 打印票据
        /// </summary>
        private void PrintShow()
        {


            //鼠标滚轮和拖动支持
            Type type = typeof(System.Windows.Forms.PrintPreviewControl);
            m_Position = type.GetField("position", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.ExactBinding);
            m_SetPositionMethod = type.GetMethod("SetPositionNoInvalidate", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.ExactBinding);
            previewControl.MouseWheel += new MouseEventHandler(previewControl_MouseWheel);
            previewControl.MouseDown += new MouseEventHandler(previewControl_MouseDown);
            previewControl.MouseUp += new MouseEventHandler(previewControl_MouseUp);
            previewControl.MouseMove += new MouseEventHandler(previewControl_MouseMove);

            xmlModel = GetXmlModel();
            // printDocument1 为 打印控件
            //设置打印用的纸张 当设置为Custom的时候，可以自定义纸张的大小，还可以选择A4,A5等常用纸型


            int width = 400;
            int hight = 500;
            try
            {
                width = int.Parse(xmlModel.PageWidth);
                hight = int.Parse(xmlModel.PageHeight);
            }
            catch { }
            this.Document.DefaultPageSettings.PaperSize = new PaperSize("Custum", width, hight);

            //根据对话框中输入的页边距设置打印文档的页边距
            Document.DefaultPageSettings.Margins = new Margins(4, 20, 4, 20);            //设置打印文档的页面大小
        
            this.Document.PrintPage += new PrintPageEventHandler(printDocument1_PrintPage);

            previewControl.Document = this.Document;
             
            this.previewControl.Show();
       
        }
      

        private void printDocument1_PrintPage(object sender, PrintPageEventArgs e)
        {
            try
            {


                string titleName = xmlModel.Title;

                string Dmmc = "";
                string Khmc = "";
                if (xmlModel.PrintName == "1")
                    Dmmc = "测试一店";


                Khmc = "测试客户";

                /*如果需要改变自己 可以在new Font(new FontFamily("黑体"),11）中的“黑体”改成自己要的字体就行了，黑体 后面的数字代表字体的大小
              System.Drawing.Brushes.Blue , 170, 10 中的 System.Drawing.Brushes.Blue 为颜色，后面的为输出的位置 */
                e.Graphics.DrawString(titleName, new Font(new FontFamily("黑体"), 13), System.Drawing.Brushes.Black, 30, 10);
                e.Graphics.DrawString(Dmmc, new Font(new FontFamily("宋体"), 8), System.Drawing.Brushes.Blue, 140, 20);

                if (Khmc != "")
                {
                    e.Graphics.DrawString("客户名称：" + Khmc, new Font(new FontFamily("黑体"), 9), System.Drawing.Brushes.Blue, 15, 30);

                }

                //项目的名称

                int line = 200;



                if (xmlModel.PageWidth != "")
                {
                    try
                    {
                        line = int.Parse(xmlModel.PageWidth) - 10;
                    }
                    catch { }
                }

                e.Graphics.DrawLine(Pens.Black, 8, 50, line, 50);

                int seat = 12;//位置
                if (xmlModel.ID == "1")
                {
                    int topWidth = 23;

                    try
                    {
                        topWidth = int.Parse(xmlModel.IDWidth);
                    }
                    catch { }

                    e.Graphics.DrawString("序号", new Font(new FontFamily("黑体"), 8), System.Drawing.Brushes.Black, seat, 52);
                    seat += topWidth;//序号的宽度 23
                }
                if (xmlModel.Spbh == "1")
                {
                    int topWidth = 35;

                    try
                    {
                        topWidth = int.Parse(xmlModel.SpbhWidth);
                    }
                    catch { }

                    e.Graphics.DrawString("编号", new Font(new FontFamily("黑体"), 8), System.Drawing.Brushes.Black, seat, 52);

                    seat += topWidth;//商品编号宽度 35
                }
                if (xmlModel.Spmc == "1")
                {
                    int topWidth = 65;
                    try
                    {
                        topWidth = int.Parse(xmlModel.SpmcWidth);
                    }
                    catch { }

                    e.Graphics.DrawString("名称", new Font(new FontFamily("黑体"), 8), System.Drawing.Brushes.Black, seat, 52);
                    seat += topWidth;//商品名称宽度 65
                }

                if (xmlModel.Spbh == "1")
                {
                    int topWidth = 35;

                    try
                    {
                        topWidth = int.Parse(xmlModel.SpbhWidth);
                    }
                    catch { }

                    e.Graphics.DrawString("货号", new Font(new FontFamily("宋体"), 8, FontStyle.Bold), System.Drawing.Brushes.Black, seat, 52);

                    seat += topWidth;//商品编号宽度 35
                }


                if (xmlModel.Sl == "1")
                {
                    int topWidth = 23;

                    try
                    {
                        topWidth = int.Parse(xmlModel.SlWidth);
                    }
                    catch { }

                    e.Graphics.DrawString("数量", new Font(new FontFamily("黑体"), 8), System.Drawing.Brushes.Black, seat, 52);

                    seat += topWidth;//数量 宽度 30
                }
                ////////////////////////////添加新字段 dw，gg，zk，zhj，pfj

                if (xmlModel.Dw == "1")
                {
                    int topWidth = 23;

                    try
                    {
                        topWidth = int.Parse(xmlModel.DwWidth);
                    }
                    catch { }

                    e.Graphics.DrawString("单位", new Font(new FontFamily("黑体"), 8), System.Drawing.Brushes.Black, seat, 52);
                    seat += topWidth;//数量 宽度 30
                }

                if (xmlModel.Gg == "1")
                {
                    int topWidth = 23;

                    try
                    {
                        topWidth = int.Parse(xmlModel.GgWidth);
                    }
                    catch { }

                    e.Graphics.DrawString("规格", new Font(new FontFamily("黑体"), 8), System.Drawing.Brushes.Black, seat, 52);
                    seat += topWidth;//数量 宽度 30
                }

                if (xmlModel.Zk == "1")
                {
                    int topWidth = 23;

                    try
                    {
                        topWidth = int.Parse(xmlModel.ZkWidth);
                    }
                    catch { }

                    SizeF zklSize = e.Graphics.MeasureString("折扣", new Font(new FontFamily("宋体"), 9));
                    float fzklSize = seat + topWidth - zklSize.Width;


                    e.Graphics.DrawString("折扣", new Font(new FontFamily("黑体"), 8), System.Drawing.Brushes.Black, fzklSize, 52);
                    seat += topWidth;//数量 宽度 30
                }
                if (xmlModel.Zhj == "1")
                {
                    int topWidth = 23;

                    try
                    {
                        topWidth = int.Parse(xmlModel.ZhjWidth);
                    }
                    catch { }

                    SizeF zhjSize = e.Graphics.MeasureString("折后价", new Font(new FontFamily("宋体"), 9));
                    float zhSize = seat + topWidth - zhjSize.Width;


                    e.Graphics.DrawString("折后价", new Font(new FontFamily("黑体"), 8), System.Drawing.Brushes.Black, zhSize, 52);
                    seat += topWidth;//数量 宽度 30
                }

                if (xmlModel.Pfj == "1")
                {
                    int topWidth = 23;

                    try
                    {
                        topWidth = int.Parse(xmlModel.PfjWidth);
                    }
                    catch { }

                    SizeF pfjSize = e.Graphics.MeasureString("批发价", new Font(new FontFamily("宋体"), 9));
                    float pSize = seat + topWidth - pfjSize.Width;



                    e.Graphics.DrawString("批发价", new Font(new FontFamily("黑体"), 8), System.Drawing.Brushes.Black, pSize, 52);
                    seat += topWidth;//数量 宽度 30
                }


                ////////////////////////////
                if (xmlModel.Dj == "1")
                {
                    int topWidth = 30;

                    try
                    {
                        topWidth = int.Parse(xmlModel.DjWidth);
                    }
                    catch { }

                    SizeF lsjSize = e.Graphics.MeasureString("单价", new Font(new FontFamily("宋体"), 9));
                    float djSize = seat + topWidth - lsjSize.Width;

                    e.Graphics.DrawString("单价", new Font(new FontFamily("黑体"), 8), System.Drawing.Brushes.Black, djSize, 52);
                    seat += topWidth;//单价 宽度 30
                }
                if (xmlModel.Xj == "1")
                {
                    int topWidth = 30;

                    try
                    {
                        topWidth = int.Parse(xmlModel.XjWidth);
                    }
                    catch { }

                    SizeF lsjSize = e.Graphics.MeasureString("金额", new Font(new FontFamily("宋体"), 9));
                    float x = seat + topWidth - lsjSize.Width;
                    //   e.Graphics.DrawString(dr["lsj"].ToString(), new Font(new FontFamily("宋体"), 9), System.Drawing.Brushes.Black, x1, high);


                    e.Graphics.DrawString("金额", new Font(new FontFamily("黑体"), 8), System.Drawing.Brushes.Black, x, 52);



                    seat += topWidth;//商品名称宽度 30
                }

                int rowIndex = 0;
                float high = 65;


                int slSite = 0;//数量位置
                int xjSite = 0;//现金位置


                DataTable spInfoData = CreateData();
                foreach (DataRow dr in spInfoData.Rows)
                {
                    int index = rowIndex + 1;
                    //项目信息

                    seat = 12;//位置
                    if (xmlModel.ID == "1")
                    {
                        int lineWidth = 23;

                        try
                        {
                            lineWidth = int.Parse(xmlModel.IDWidth);
                        }
                        catch { }

                        e.Graphics.DrawString(index.ToString(), new Font(new FontFamily("宋体"), 9), System.Drawing.Brushes.Black, seat, high);
                        seat += lineWidth;//序号的宽度 默认 23
                    }
                    if (xmlModel.Spbh == "1")
                    {
                        int lineWidth = 65;

                        try
                        {
                            lineWidth = int.Parse(xmlModel.SpbhWidth);
                        }
                        catch { }

                        e.Graphics.DrawString(dr["spbh"].ToString(), new Font(new FontFamily("宋体"), 9), System.Drawing.Brushes.Black, seat, high);

                        seat += lineWidth;//商品编号宽度 65
                    }
                    if (xmlModel.Spmc == "1")
                    {
                        int lineWidth = 65;

                        try
                        {
                            lineWidth = int.Parse(xmlModel.SpmcWidth);
                        }
                        catch { }

                        e.Graphics.DrawString(dr["spmc"].ToString(), new Font(new FontFamily("宋体"), 9), System.Drawing.Brushes.Black, seat, high);
                        seat += lineWidth;//商品名称宽度 65
                    }
                    if (xmlModel.Sl == "1")
                    {
                        int lineWidth = 30;

                        try
                        {
                            lineWidth = int.Parse(xmlModel.SlWidth);
                        }
                        catch { }

                        e.Graphics.DrawString(dr["sl"].ToString(), new Font(new FontFamily("宋体"), 9), System.Drawing.Brushes.Black, seat, high);
                        slSite = seat;
                        seat += lineWidth;//数量 宽度 30
                    }

                    /////////////////////添加新字段 dw，gg，zk，zhj，pfj
                    if (xmlModel.Dw == "1")
                    {
                        int lineWidth = 30;

                        try
                        {
                            lineWidth = int.Parse(xmlModel.DwWidth);
                        }
                        catch { }

                        e.Graphics.DrawString(dr["dw"].ToString(), new Font(new FontFamily("宋体"), 9), System.Drawing.Brushes.Black, seat, high);
                        slSite = seat;
                        seat += lineWidth;//数量 宽度 30
                    }

                    if (xmlModel.Gg == "1")
                    {
                        int lineWidth = 30;

                        try
                        {
                            lineWidth = int.Parse(xmlModel.GgWidth);
                        }
                        catch { }

                        e.Graphics.DrawString(dr["gg"].ToString(), new Font(new FontFamily("宋体"), 9), System.Drawing.Brushes.Black, seat, high);
                        slSite = seat;
                        seat += lineWidth;//数量 宽度 30
                    }

                    if (xmlModel.Zk == "1")
                    {
                        int lineWidth = 30;

                        try
                        {
                            lineWidth = int.Parse(xmlModel.ZkWidth);
                        }
                        catch { }

                        string zk = dr["zk"].ToString();
                        SizeF zkSize = e.Graphics.MeasureString(zk, new Font(new FontFamily("宋体"), 9));
                        float fzkSize = seat + lineWidth - zkSize.Width;


                        e.Graphics.DrawString(zk, new Font(new FontFamily("宋体"), 9), System.Drawing.Brushes.Black, fzkSize, high);
                        slSite = seat;
                        seat += lineWidth;//数量 宽度 30
                    }

                    if (xmlModel.Zhj == "1")
                    {
                        int lineWidth = 30;

                        try
                        {
                            lineWidth = int.Parse(xmlModel.ZhjWidth);
                        }
                        catch { }

                        string zhj = dr["zhj"].ToString();
                        SizeF zhjSize = e.Graphics.MeasureString(zhj, new Font(new FontFamily("宋体"), 9));
                        float fzhjSize = seat + lineWidth - zhjSize.Width;


                        e.Graphics.DrawString(zhj, new Font(new FontFamily("宋体"), 9), System.Drawing.Brushes.Black, fzhjSize, high);
                        slSite = seat;
                        seat += lineWidth;//数量 宽度 30
                    }

                    if (xmlModel.Pfj == "1")
                    {
                        int lineWidth = 30;

                        try
                        {
                            lineWidth = int.Parse(xmlModel.PfjWidth);
                        }
                        catch { }

                        string pfj = dr["pfj"].ToString();
                        SizeF pfjSize = e.Graphics.MeasureString(pfj, new Font(new FontFamily("宋体"), 9));
                        float fpfjSize = seat + lineWidth - pfjSize.Width;


                        e.Graphics.DrawString(pfj, new Font(new FontFamily("宋体"), 9), System.Drawing.Brushes.Black, fpfjSize, high);
                        slSite = seat;
                        seat += lineWidth;//数量 宽度 30
                    }
                    /////////////////////

                    if (xmlModel.Dj == "1")
                    {
                        int lineWidth = 30;

                        try
                        {
                            lineWidth = int.Parse(xmlModel.DjWidth);
                        }
                        catch { }

                        SizeF lsjSize = e.Graphics.MeasureString(dr["lsj"].ToString(), new Font(new FontFamily("宋体"), 9));
                        float x1 = seat + lineWidth - lsjSize.Width;
                        e.Graphics.DrawString(dr["lsj"].ToString(), new Font(new FontFamily("宋体"), 9), System.Drawing.Brushes.Black, x1, high);


                        // e.Graphics.DrawString(dr["lsj"].ToString(), new Font(new FontFamily("宋体"), 9), System.Drawing.Brushes.Black, seat, high);
                        seat += lineWidth;//单价 宽度 30
                    }
                    if (xmlModel.Xj == "1")
                    {
                        int lineWidth = 30;

                        try
                        {
                            lineWidth = int.Parse(xmlModel.XjWidth);
                        }
                        catch { }

                        string ssje = double.Parse(dr["ssje"].ToString()).ToString("0.00");
                        if (ssje.IndexOf('.') == -1)
                        {
                            ssje = ssje + ".00";
                        }
                        SizeF ssjeSize = e.Graphics.MeasureString(ssje, new Font(new FontFamily("宋体"), 9));
                        float x3 = seat + lineWidth - ssjeSize.Width;
                        e.Graphics.DrawString(ssje, new Font(new FontFamily("宋体"), 9), System.Drawing.Brushes.Black, x3, high);



                        //   e.Graphics.DrawString(dr["ssje"].ToString(), new Font(new FontFamily("宋体"), 9), System.Drawing.Brushes.Black, seat, high);
                        xjSite = seat;
                        seat += lineWidth;//商品名称宽度 30
                    }

                    high += 13;

                    rowIndex++;
                }

                e.Graphics.DrawString("合计：", new Font(new FontFamily("黑体"), 8), System.Drawing.Brushes.Black, 12, high);


                string allSl = GetAllSl(spInfoData).ToString("0.00");

                SizeF allSlSize = e.Graphics.MeasureString(allSl, new Font(new FontFamily("宋体"), 9));
                float fallSlSize = seat + line - allSlSize.Width;



                e.Graphics.DrawString(allSl, new Font(new FontFamily("黑体"), 8), System.Drawing.Brushes.Black, slSite, high);
                e.Graphics.DrawString(GetAllMoney(spInfoData).ToString(), new Font(new FontFamily("黑体"), 8), System.Drawing.Brushes.Black, xjSite, high);



                high += 13;//合计行

                e.Graphics.DrawLine(Pens.Black, 8, high, line, high);
                high = high + 10;//显示下分割线
                e.Graphics.DrawString("数量：" + GetAllSl(spInfoData), new Font(new FontFamily("黑体"), 8), System.Drawing.Brushes.Black, 13, high);
                e.Graphics.DrawString("合计：", new Font(new FontFamily("黑体"), 8), System.Drawing.Brushes.Black, 100, high);

                high = high + 20;//付现
                e.Graphics.DrawString("付现：101", new Font(new FontFamily("黑体"), 12), System.Drawing.Brushes.Black, 13, high);
                high = high + 20;//找零
                e.Graphics.DrawString("找零：9", new Font(new FontFamily("黑体"), 12), System.Drawing.Brushes.Black, 13, high);
                high = high + 20;//收银员
                e.Graphics.DrawString("收银员：闵杰", new Font(new FontFamily("黑体"), 8), System.Drawing.Brushes.Black, 13, high);

                if (xmlModel.PrintTime == "1")
                {
                    high = high + 15;//时间

                    e.Graphics.DrawString("时间：" + DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"), new Font(new FontFamily("黑体"), 8), System.Drawing.Brushes.Black, 12, high);
                }


                if (xmlModel.ctxtYw1 != "")
                {
                    high = high + 15;//时间
                    int width = GetYWSite(xmlModel.key1);
                    e.Graphics.DrawString(xmlModel.ctxtYw1, new Font(new FontFamily("黑体"), 8), System.Drawing.Brushes.Black, width, high);

                }
                if (xmlModel.ctxtYw2 != "")
                {
                    high = high + 15;//时间
                    int width = GetYWSite(xmlModel.key2);
                    e.Graphics.DrawString(xmlModel.ctxtYw2, new Font(new FontFamily("黑体"), 8), System.Drawing.Brushes.Black, width, high);
                }
                if (xmlModel.ctxtYw3 != "")
                {
                    high = high + 15;//时间
                    int width = GetYWSite(xmlModel.key3);
                    e.Graphics.DrawString(xmlModel.ctxtYw3, new Font(new FontFamily("黑体"), 8), System.Drawing.Brushes.Black, width, high);
                }
                if (xmlModel.ctxtYw4 != "")
                {
                    high = high + 15;//时间
                    int width = GetYWSite(xmlModel.key4);
                    e.Graphics.DrawString(xmlModel.ctxtYw4, new Font(new FontFamily("黑体"), 8), System.Drawing.Brushes.Black, width, high);
                }
                if (xmlModel.ctxtYw5 != "")
                {
                    high = high + 15;//时间
                    int width = GetYWSite(xmlModel.key5);
                    e.Graphics.DrawString(xmlModel.ctxtYw5, new Font(new FontFamily("黑体"), 8), System.Drawing.Brushes.Black, width, high);
                }
                if (xmlModel.ctxtYw6 != "")
                {
                    high = high + 15;//时间
                    int width = GetYWSite(xmlModel.key6);
                    e.Graphics.DrawString(xmlModel.ctxtYw6, new Font(new FontFamily("黑体"), 8), System.Drawing.Brushes.Black, width, high);
                }

            }
            catch
            {
                MessageBox.Show(this, "打印出错！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
        private XmlModel GetXmlModel()
        {

            XmlDocument doc = new XmlDocument();
            string path = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase) + @"\PosConfig.xml";
            doc.Load(path);    //加载Xml文件  
            XmlElement rootElem = doc.DocumentElement;   //获取根节点  
            XmlModel model = new XmlModel();

            XmlNodeList InfoXmls = rootElem.ChildNodes;//继续获取  根节点 所有的子节点


            foreach (XmlNode xn1 in InfoXmls)//遍历
            {

                XmlElement xe = (XmlElement)xn1;//将子节点类型转换为XmlElement类型
                if (xe.GetAttribute("name") == "head")// 查找到表头
                {
                    XmlNodeList nls = xe.ChildNodes;
                    foreach (XmlNode xnn1 in nls)
                    {
                        XmlElement xe2 = (XmlElement)xnn1;

                        if (xe2.Name == "title")
                        {
                            model.Title = xe2.InnerText;

                        }
                        else if (xe2.Name == "printname")
                        {
                            model.PrintName = xe2.InnerText;

                        }
                        else if (xe2.Name == "printtime")
                        {
                            model.PrintTime = xe2.InnerText;
                        }
                        else if (xe2.Name == "site")
                        {
                            model.Site = xe2.InnerText;
                        }

                        else if (xe2.Name == "addinfo1")
                        {
                            model.Addinfo1 = xe2.InnerText;
                        }

                        else if (xe2.Name == "addinfo2")
                        {
                            model.Addinfo2 = xe2.InnerText;
                        }


                        else if (xe2.Name == "pageheight")
                        {
                            model.PageHeight = xe2.InnerText;
                        }

                        else if (xe2.Name == "pagewidth")
                        {
                            model.PageWidth = xe2.InnerText;
                        }


                    }


                }
                else if (xe.GetAttribute("name") == "body")// 查找到表体
                {
                    XmlNodeList nls = xe.ChildNodes;
                    foreach (XmlNode xnn1 in nls)
                    {
                        XmlElement xe2 = (XmlElement)xnn1;

                        if (xe2.Name == "ID")
                        {
                            model.ID = xe2.InnerText;
                            model.IDWidth = xe2.GetAttribute("Width");
                        }
                        else if (xe2.Name == "Spbh")
                        {
                            model.Spbh = xe2.InnerText;
                            model.SpbhWidth = xe2.GetAttribute("Width");
                        }
                        else if (xe2.Name == "Spmc")
                        {
                            model.Spmc = xe2.InnerText;
                            model.SpmcWidth = xe2.GetAttribute("Width");

                        }
                        else if (xe2.Name == "Sl")
                        {
                            model.Sl = xe2.InnerText;
                            model.SlWidth = xe2.GetAttribute("Width");

                        }
                        ////////////新增加的字段dw，gg，zk，zhj，pfj
                        else if (xe2.Name == "dw")
                        {
                            model.Dw = xe2.InnerText;
                            model.DwWidth = xe2.GetAttribute("Width");

                        }
                        else if (xe2.Name == "gg")
                        {
                            model.Gg = xe2.InnerText;
                            model.GgWidth = xe2.GetAttribute("Width");

                        }
                        else if (xe2.Name == "zk")
                        {
                            model.Zk = xe2.InnerText;
                            model.ZkWidth = xe2.GetAttribute("Width");

                        }
                        else if (xe2.Name == "zhj")
                        {
                            model.Zhj = xe2.InnerText;
                            model.ZhjWidth = xe2.GetAttribute("Width");

                        }
                        else if (xe2.Name == "pfj")
                        {
                            model.Pfj = xe2.InnerText;
                            model.PfjWidth = xe2.GetAttribute("Width");

                        }
                        ///////////////


                        else if (xe2.Name == "Dj")
                        {
                            model.Dj = xe2.InnerText;
                            model.DjWidth = xe2.GetAttribute("Width");

                        }
                        else if (xe2.Name == "Xj")
                        {
                            model.Xj = xe2.InnerText;
                            model.XjWidth = xe2.GetAttribute("Width");
                        }
                    }


                }
                else if (xe.GetAttribute("name") == "down")// 查找到表尾
                {
                    XmlNodeList nls = xe.ChildNodes;
                    foreach (XmlNode xnn1 in nls)
                    {

                        XmlElement xe2 = (XmlElement)xnn1;

                        if (xe2.Name == "ctxtYw1")
                        {
                            model.key1 = xe2.GetAttribute("key");
                            model.ctxtYw1 = xe2.InnerText;
                        }
                        if (xe2.Name == "ctxtYw2")
                        {
                            model.key2 = xe2.GetAttribute("key");
                            model.ctxtYw2 = xe2.InnerText;
                        }
                        if (xe2.Name == "ctxtYw3")
                        {
                            model.key3 = xe2.GetAttribute("key");
                            model.ctxtYw3 = xe2.InnerText;
                        }
                        if (xe2.Name == "ctxtYw4")
                        {
                            model.key4 = xe2.GetAttribute("key");
                            model.ctxtYw4 = xe2.InnerText;
                        }
                        if (xe2.Name == "ctxtYw5")
                        {
                            model.key5 = xe2.GetAttribute("key");
                            model.ctxtYw5 = xe2.InnerText;
                        }
                        if (xe2.Name == "ctxtYw6")
                        {
                            model.key6 = xe2.GetAttribute("key");
                            model.ctxtYw6 = xe2.InnerText;
                        }
                    }
                }
            }
            return model;
        }


        /// <summary>
        /// 数量合计
        /// </summary>
        /// <returns></returns>
        private decimal GetAllSl(DataTable dt)
        {
            decimal allSl = 0;
            decimal sl = 0;
            foreach (DataRow dr in dt.Rows)
            {
                if (dr["sl"].ToString() != "")
                    sl = decimal.Parse(dr["sl"].ToString());
                allSl += sl;
            }
            return allSl;
        }


        /// <summary>
        /// 实收金额合计
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        private decimal GetAllMoney(DataTable dt)
        {
            decimal allMoney = 0;
            decimal xj = 0;
            foreach (DataRow dr in dt.Rows)
            {
                if (dr["ssje"].ToString() != "")
                    xj = decimal.Parse(dr["ssje"].ToString());
                allMoney += xj;
            }

            //if (allMoney < 0)
            //    allMoney = 0;
            return allMoney;
        }

        /// <summary>
        /// 获取页尾位置
        /// </summary>
        /// <param name="site">0 居左，1 居中，2 居右</param>
        /// <returns></returns>
        private int GetYWSite(string site)
        {
            int intSite = 0;
            if (site == "0")
            {
                intSite = 12;
            }
            else if (site == "0")
            {
                intSite = 120;
            }
            else
            {
                intSite = 250;
            }
            return intSite;

        }

        #endregion



        public DataTable CreateData()
        {
            DataTable dt = new DataTable();
            //dw，gg，zk，zhj，pfj
            dt.Columns.Add("spbh");
            dt.Columns.Add("spmc");
            dt.Columns.Add("sl");
            dt.Columns.Add("dw");

            dt.Columns.Add("gg");
            dt.Columns.Add("zk");
            dt.Columns.Add("zhj");
            dt.Columns.Add("pfj");

            dt.Columns.Add("lsj");

            dt.Columns.Add("ssje");

            for (int i = 0; i < 30; i++)
            {
                DataRow dr = dt.NewRow();
                dr["spbh"] = "A0001" + i;
                dr["spmc"] = "商品名称" + i;
                dr["sl"] = ++i;
                dr["dw"] = "个";
                dr["gg"] = "1";

                dr["zk"] = "0.85";
                dr["zhj"] = (++i) * 5;
                dr["pfj"] = (++i) * 4.5;

                dr["lsj"] = (++i) * 7;
                dr["ssje"] = (++i) * 7;
                dt.Rows.Add(dr);

            }

            return dt;
        }

        #region 鼠标滚轮和拖动支持事件

        private void previewControl_MouseDown(object sender, MouseEventArgs e)
        {
            isMouseDown = true;
            startPosition = new Point(e.X, e.Y);
            curPos = (Point)(m_Position.GetValue(previewControl));
        }

        private void previewControl_MouseUp(object sender, MouseEventArgs e)
        {
            isMouseDown = false;
            endPosition = new Point(e.X, e.Y);
            m_SetPositionMethod.Invoke(previewControl, new object[] { new Point(curPos.X + (startPosition.X - endPosition.X), curPos.Y + (startPosition.Y - endPosition.Y)) });
        }

        private void previewControl_MouseMove(object sender, MouseEventArgs e)
        {
            if (isMouseDown == true)
            {
                endPosition = new Point(e.X, e.Y);
                m_SetPositionMethod.Invoke(previewControl, new object[] { new Point(curPos.X + (startPosition.X - endPosition.X), curPos.Y + (startPosition.Y - endPosition.Y)) });
            }
        }
        private void previewControl_MouseWheel(object sender, MouseEventArgs e)
        {
            if (!SystemInformation.MouseWheelPresent)
            {
                //If have no wheel   
                return;
            }
            int scrollAmount;
            float amount = Math.Abs(e.Delta) / SystemInformation.MouseWheelScrollDelta;
            amount *= SystemInformation.MouseWheelScrollLines;
            amount *= 12;//Row height   
            amount *= (float)previewControl.Zoom;//Zoom Rate   
            if (e.Delta < 0)
            {
                scrollAmount = (int)amount;
            }
            else
            {
                scrollAmount = -(int)amount;
            }
            Point curPos = (Point)(m_Position.GetValue(previewControl));
            m_SetPositionMethod.Invoke(previewControl, new object[] { new Point(curPos.X + 0, curPos.Y + scrollAmount) });
        }

        #endregion

        private void PrintShowDialog_Load(object sender, EventArgs e)
        {
            PrintShow();
        }

        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            if (this.previewControl.Zoom == 1.0)
            {
                this.previewControl.Zoom = 0.75;
            }
            else
            {
                this.previewControl.Zoom -= 0.25;
            }

        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            Document.Print();
        }

        private void toolStripButton3_Click(object sender, EventArgs e)
        {

        }

        private void toolStripButton4_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void toolStripButton3_Click_1(object sender, EventArgs e)
        {
            this.previewControl.Zoom += 0.25;
        }


    }
}
